<template>
    <div class="notice-box">
      <div class="notice-title">
        系统消息
      </div>
      <div class="notice-body">
        <div class="body-title">
          <div class="title-index">

          </div>
          <div class="title-content">
            消息内容
          </div>
          <div class="title-more">
            创建时间
          </div>
        </div>
        <el-scrollbar height="31vh">
        <div class="notice-item" v-for="(item,i) in noticeList" :key="i"  v-if="noticeList.length">
          <div class="notice-index">
            {{ i + 1 }}
          </div>
          <div class="notice-content" @click="toDetails(item.id)">
            {{item.content}}
          </div>
          <div class="notice-more">
            {{computeDate(item.created_at)}}
          </div>
        </div>
        <div class="notice-body"
          style="padding-top: 13vh;background-color:#fff;height: 35vh;box-sizing: border-box" v-else>
          暂无未读消息
        </div>
        </el-scrollbar>
      </div>
    </div>
</template>

<script setup>
import {onMounted, ref} from "vue"
import {getMSGList} from "@/api/index.js"
import {useRouter} from "vue-router"
import {createMsg} from "@/lib/utils"

const userId = ref()
const noticeList = ref([])
const router = useRouter()

onMounted(()=>{
  getUserId()
  setTimeout(()=>{
    getNoticeList()
  })
})


const getUserId = () => {
  userId.value = JSON.parse(localStorage.getItem('syhyDocManSys')).id
}

const getNoticeList = async () => {
  try {
    const {data:res} = await getMSGList({
      userId:userId.value
    })
    if (res.code === 200){
      let unread = []
      res.data.forEach((item)=>{
        if (item.status === 0){
          noticeList.value.push(item)
        }
      })
      unread = noticeList.value
    } else if (res.code === 201) {
      noticeList.value = []
    } else {
      noticeList.value = []
    }
  } catch (e) {
    console.log(e)
  }
}

const computeDate = (date) => {
  return date.substring(0,10)
}

const toDetails = (id) => {
  router.push({
    path:'/msg',
    query:{
      id
    }
  })
}

</script>

<style scoped lang="less">
.notice-box{
  width: 33vw;
  height: 47vh;
  border-radius: 10px;
  box-shadow: 0 0 8px 8px lightgray;
  background-color: #fafafa;
  position: absolute;
  left: 65.5vw;
  top: 2vh;
  .notice-title{
    height: 5vh;
    border-bottom: 1px solid #e4eaeb;
    box-sizing: border-box;
    line-height: 5vh;
    font-size: 2vh;
    padding-left: 1vw;
    text-align: left;
    font-weight: 600;
  }
  .notice-body{
    height: 31.5vh;
    max-height: 31.5vh;
    .notice-item{
      font-size: 1.5vh;
      background-color: rgb(255,255,255);
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      height: 5vh;
      line-height: 5vh;
      border: 1px solid #ebeef5;
      width: 100%;
      &:hover{
        background-color: rgb(245,247,250);
      }
      .notice-index{
        width: 10%;
        text-align: center;
        border-right: 1px solid #ebeef5;
      }
      .notice-content{
        width: 60%;
        text-align: center;
        border-right: 1px solid #ebeef5;
        &:hover{
          color: #1E90FF;
          cursor: pointer;
          text-decoration: underline;
        }
      }
      .notice-more{
        width: 30%;
        text-align: center;
      }
    }
    .body-title{
      background-color: rgb(255,255,255);
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      height: 4vh;
      line-height: 4vh;
      width: 100%;
      font-size: 1.6vh;
      font-weight: 600;
      .title-index{
        width: 10%;
        text-align: center;
      }
      .title-content{
        width: 60%;
        text-align: center;
      }
      .title-more{
        width: 30%;
        text-align: center;
      }
    }
  }
}
</style>
